package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.*;
import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping(value = "/FileMakerConvertPDFs/")
public class FileMakerConvertPDFsController {
    //获取doc目录的磁盘路径
    private final String dir = GetDirPathUtil.getDirPath() + "static/doc/";

    public FileMakerConvertPDFsController() throws IOException {
    }

    @RequestMapping(value = "convert")
    public void convert(HttpServletRequest request, HttpServletResponse response, int id) throws IOException {
        String docName = "doc0" + id + ".docx";
        String pdfName = "doc0" + id + ".pdf";

        FileMakerCtrl fmCtrl = new FileMakerCtrl(request);
        fmCtrl.fillDocumentAsPDF("/doc/FileMakerConvertPDFs/" + docName, DocumentOpenType.Word, pdfName);
        response.getOutputStream().print(fmCtrl.getHtml());
    }


    @RequestMapping(value = "edit")
    public String edit(HttpServletRequest request, int id) {
        String docName = "doc0" + id + ".docx";

        PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
        poCtrl1.webOpen("/doc/FileMakerConvertPDFs/" + docName, OpenModeType.docNormalEdit, "张三");
        return poCtrl1.getHtml();
    }

    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "FileMakerConvertPDFs/" + fs.getFileName());
        //设置自定义保存结果，用来返回给前端页面，setCustomSaveResult的参数也可以是json字符串类型
        //设置文件的保存结果
        fs.setCustomSaveResult("ok");
        fs.close();
    }

}
